﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Runtime.InteropServices;
using System.Resources;
using System.Collections;
using System.Data.SqlClient;
using System.Data.Odbc;
using System.Data.OleDb;
using System.Data.OracleClient;
using System.Reflection;
using Microsoft.Win32;
using System.Web.UI.WebControls;
using System.Web;

namespace Srvtools
{
    public partial class frmWebMultiLanguageEditor : Form
    {
        private WebMultiLanguage MultiLanguage;
        private ResourceManagerEditorLanguageSelect rmels = null;
        private string xmlName = "";
        private int identification;
        private bool[] languageselected = new bool[8] { false, false, false, false, false, false, false, false };
        private object[] DBInfo;
        public frmWebMultiLanguageEditor(WebMultiLanguage multiLanguage)
        {
            InitializeComponent();
            MultiLanguage = multiLanguage;
            DBInfo = ConfirmDataBaseStyle(this.MultiLanguage.DataBase);
        }

        private void frmWebMultiLanguageEditor_Load(object sender, EventArgs e)
        {
            xmlName = EditionDifference.ActiveDocumentFullName() + ".xml";
            string strident = xmlName + MultiLanguage.Site.Name;
            identification = strident.GetHashCode();
            this.Text = string.Format("Multilanguage Editor({0})", identification);
            if (File.Exists(xmlName))
            {
                XmlDocument xml = new XmlDocument();
                xml.Load(xmlName);
                XmlNode nodeLanguage = xml.DocumentElement.SelectSingleNode("MultiLanguage");
                if (nodeLanguage != null)
                {
                    string strLanguage = nodeLanguage.Attributes["language"].InnerText;
                    if (strLanguage != string.Empty)
                    {
                        //read from xml find language node;
                        if (strLanguage.Contains("EN"))
                        {
                            languageselected[0] = true;
                        }
                        if (strLanguage.Contains("CHT"))
                        {
                            languageselected[1] = true;
                        }
                        if (strLanguage.Contains("CHS"))
                        {
                            languageselected[2] = true;
                        }
                        if (strLanguage.Contains("HK"))
                        {
                            languageselected[3] = true;
                        }
                        if (strLanguage.Contains("JA"))
                        {
                            languageselected[4] = true;
                        }
                        if (strLanguage.Contains("KO"))
                        {
                            languageselected[5] = true;
                        }
                        if (strLanguage.Contains("LAN1"))
                        {
                            languageselected[6] = true;
                        }
                        if (strLanguage.Contains("LAN2"))
                        {
                            languageselected[7] = true;
                        }
                    }
                    else
                    {
                        rmels = new ResourceManagerEditorLanguageSelect(languageselected);
                        if (rmels.ShowDialog() == DialogResult.OK)
                        {
                            languageselected = rmels.SetSelectedLang();
                        }
                    }
                }
                else
                {
                    InitialXML(false);
                    rmels = new ResourceManagerEditorLanguageSelect(languageselected);
                    if (rmels.ShowDialog() == DialogResult.OK)
                    {
                        languageselected = rmels.SetSelectedLang();
                    }
                }

            }
            else
            {
                InitialXML(true);
                rmels = new ResourceManagerEditorLanguageSelect(languageselected);
                if (rmels.ShowDialog() == DialogResult.OK)
                {
                    languageselected = rmels.SetSelectedLang();
                }
            }
        }

        private void InitialXML(bool creatXml)
        {
            if (!creatXml)
            {
                File.Delete(xmlName);
            }

            FileStream xmlstream = new FileStream(xmlName, FileMode.Create);
            XmlTextWriter w = new XmlTextWriter(xmlstream, new System.Text.UnicodeEncoding());
            w.Formatting = Formatting.Indented;
            w.WriteStartElement("Infolight");
            w.WriteStartElement("EN");
            w.WriteEndElement();
            w.WriteStartElement("CHT");
            w.WriteEndElement();
            w.WriteStartElement("CHS");
            w.WriteEndElement();
            w.WriteStartElement("HK");
            w.WriteEndElement();
            w.WriteStartElement("JA");
            w.WriteEndElement();
            w.WriteStartElement("KO");
            w.WriteEndElement();
            w.WriteStartElement("LAN1");
            w.WriteEndElement();
            w.WriteStartElement("LAN2");
            w.WriteEndElement();
            w.WriteEndElement();

            w.Close();
            xmlstream.Close();

            XmlDocument xml = new XmlDocument();
            try
            {
                xml.Load(xmlName);
                XmlElement nodeLanguage = xml.CreateElement("MultiLanguage");
                XmlAttribute attLanguage = xml.CreateAttribute("language");
                attLanguage.Value = "";
                nodeLanguage.Attributes.Append(attLanguage);
                xml.DocumentElement.PrependChild(nodeLanguage);
                xml.Save(xmlName);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        private void InitialXMLLanguage(XmlDocument xml, bool[] bllanguage)
        {
            string strlanguage = "";
            if (bllanguage[0])
            {
                strlanguage += "EN;";
            }
            if (bllanguage[1])
            {
                strlanguage += "CHT;";
            }
            if (bllanguage[2])
            {
                strlanguage += "CHS;";
            }
            if (bllanguage[3])
            {
                strlanguage += "HK;";
            }
            if (bllanguage[4])
            {
                strlanguage += "JA;";
            }
            if (bllanguage[5])
            {
                strlanguage += "KO;";
            }
            if (bllanguage[6])
            {
                strlanguage += "LAN1;";
            }
            if (bllanguage[7])
            {
                strlanguage += "LAN2;";
            }
            if (strlanguage != "")
            {
                strlanguage = strlanguage.Substring(0, strlanguage.LastIndexOf(';'));
            }

            XmlNode nodeLanguage = xml.DocumentElement.SelectSingleNode("MultiLanguage");
            nodeLanguage.Attributes["language"].InnerText = strlanguage;
            xml.Save(xmlName);
        }


        private void SetValue(XmlDocument xml, XmlNode nodelanguage, string fieldname, string value)
        {
            XmlNode nodefieldname;
            try
            {
                nodefieldname = nodelanguage.SelectSingleNode("lan" + fieldname);
                nodefieldname.Attributes["value"].Value = value;
            }
            catch
            {
                XmlElement nodeelement = xml.CreateElement("lan" + fieldname);
                XmlAttribute attfield = xml.CreateAttribute("field");
                attfield.Value = fieldname;
                XmlAttribute attvalue = xml.CreateAttribute("value");
                attvalue.Value = value;
                nodeelement.Attributes.Append(attfield);
                nodeelement.Attributes.Append(attvalue);
                nodelanguage.AppendChild(nodeelement);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(xmlName);
            XmlNode nodelanguage;
            InitialXMLLanguage(xml, languageselected);

            if (languageselected[0])
            {
                nodelanguage = xml.DocumentElement.SelectSingleNode("EN");
                if (nodelanguage == null)
                {
                    XmlElement newnode = xml.CreateElement("EN");
                    xml.DocumentElement.AppendChild(newnode);
                    nodelanguage = xml.DocumentElement.SelectSingleNode("EN");
                }
                foreach (DataGridViewRow row in this.dgViewLanguage.Rows)
                {
                    if (dgViewLanguage["EN", row.Index].Value != null && dgViewLanguage["EN", row.Index].Value.ToString() != string.Empty)
                    {
                        string strName = (string)dgViewLanguage["KEYS", row.Index].Value;
                        string strValue = (string)dgViewLanguage["EN", row.Index].Value;
                        SetValue(xml, nodelanguage, strName, strValue);
                    }
                }
            }
            if (languageselected[1])
            {
                nodelanguage = xml.DocumentElement.SelectSingleNode("CHT");
                if (nodelanguage == null)
                {
                    XmlElement newnode = xml.CreateElement("CHT");
                    xml.DocumentElement.AppendChild(newnode);
                    nodelanguage = xml.DocumentElement.SelectSingleNode("CHT");
                }
                foreach (DataGridViewRow row in this.dgViewLanguage.Rows)
                {
                    if (dgViewLanguage["CHT", row.Index].Value != null && dgViewLanguage["CHT", row.Index].Value.ToString() != string.Empty)
                    {
                        string strName = (string)dgViewLanguage["KEYS", row.Index].Value;
                        string strValue = (string)dgViewLanguage["CHT", row.Index].Value;
                        SetValue(xml, nodelanguage, strName, strValue);
                    }
                }
            }
            if (languageselected[2])
            {
                nodelanguage = xml.DocumentElement.SelectSingleNode("CHS");
                if (nodelanguage == null)
                {
                    XmlElement newnode = xml.CreateElement("CHS");
                    xml.DocumentElement.AppendChild(newnode);
                    nodelanguage = xml.DocumentElement.SelectSingleNode("CHS");
                }
                foreach (DataGridViewRow row in this.dgViewLanguage.Rows)
                {
                    if (dgViewLanguage["CHS", row.Index].Value != null && dgViewLanguage["CHS", row.Index].Value.ToString() != string.Empty)
                    {
                        string strName = (string)dgViewLanguage["KEYS", row.Index].Value;
                        string strValue = (string)dgViewLanguage["CHS", row.Index].Value;
                        SetValue(xml, nodelanguage, strName, strValue);
                    }
                }
            }
            if (languageselected[3])
            {
                nodelanguage = xml.DocumentElement.SelectSingleNode("HK");
                if (nodelanguage == null)
                {
                    XmlElement newnode = xml.CreateElement("HK");
                    xml.DocumentElement.AppendChild(newnode);
                    nodelanguage = xml.DocumentElement.SelectSingleNode("HK");
                }
                foreach (DataGridViewRow row in this.dgViewLanguage.Rows)
                {
                    if (dgViewLanguage["HK", row.Index].Value != null && dgViewLanguage["HK", row.Index].Value.ToString() != string.Empty)
                    {
                        string strName = (string)dgViewLanguage["KEYS", row.Index].Value;
                        string strValue = (string)dgViewLanguage["HK", row.Index].Value;
                        SetValue(xml, nodelanguage, strName, strValue);
                    }
                }
            }
            if (languageselected[4])
            {
                nodelanguage = xml.DocumentElement.SelectSingleNode("JA");
                if (nodelanguage == null)
                {
                    XmlElement newnode = xml.CreateElement("JA");
                    xml.DocumentElement.AppendChild(newnode);
                    nodelanguage = xml.DocumentElement.SelectSingleNode("JA");
                }
                foreach (DataGridViewRow row in this.dgViewLanguage.Rows)
                {
                    if (dgViewLanguage["JA", row.Index].Value != null && dgViewLanguage["JA", row.Index].Value.ToString() != string.Empty)
                    {
                        string strName = (string)dgViewLanguage["KEYS", row.Index].Value;
                        string strValue = (string)dgViewLanguage["JA", row.Index].Value;
                        SetValue(xml, nodelanguage, strName, strValue);
                    }
                }
            }
            if (languageselected[5])
            {
                nodelanguage = xml.DocumentElement.SelectSingleNode("KO");
                if (nodelanguage == null)
                {
                    XmlElement newnode = xml.CreateElement("KO");
                    xml.DocumentElement.AppendChild(newnode);
                    nodelanguage = xml.DocumentElement.SelectSingleNode("KO");
                }
                foreach (DataGridViewRow row in this.dgViewLanguage.Rows)
                {
                    if (dgViewLanguage["KO", row.Index].Value != null && dgViewLanguage["KO", row.Index].Value.ToString() != string.Empty)
                    {
                        string strName = (string)dgViewLanguage["KEYS", row.Index].Value;
                        string strValue = (string)dgViewLanguage["KO", row.Index].Value;
                        SetValue(xml, nodelanguage, strName, strValue);
                    }
                }
            }
            if (languageselected[6])
            {
                nodelanguage = xml.DocumentElement.SelectSingleNode("LAN1");
                if (nodelanguage == null)
                {
                    XmlElement newnode = xml.CreateElement("LAN1");
                    xml.DocumentElement.AppendChild(newnode);
                    nodelanguage = xml.DocumentElement.SelectSingleNode("LAN1");
                }
                foreach (DataGridViewRow row in this.dgViewLanguage.Rows)
                {
                    if (dgViewLanguage["LAN1", row.Index].Value != null && dgViewLanguage["LAN1", row.Index].Value.ToString() != string.Empty)
                    {
                        string strName = (string)dgViewLanguage["KEYS", row.Index].Value;
                        string strValue = (string)dgViewLanguage["LAN1", row.Index].Value;
                        SetValue(xml, nodelanguage, strName, strValue);
                    }
                }
            }
            if (languageselected[7])
            {
                nodelanguage = xml.DocumentElement.SelectSingleNode("LAN2");
                if (nodelanguage == null)
                {
                    XmlElement newnode = xml.CreateElement("LAN2");
                    xml.DocumentElement.AppendChild(newnode);
                    nodelanguage = xml.DocumentElement.SelectSingleNode("LAN2");
                }
                foreach (DataGridViewRow row in this.dgViewLanguage.Rows)
                {
                    if (dgViewLanguage["LAN2", row.Index].Value != null && dgViewLanguage["LAN2", row.Index].Value.ToString() != string.Empty)
                    {
                        string strName = (string)dgViewLanguage["KEYS", row.Index].Value;
                        string strValue = (string)dgViewLanguage["LAN2", row.Index].Value;
                        SetValue(xml, nodelanguage, strName, strValue);
                    }
                }
            }

            xml.Save(xmlName);
            writeDB();
            this.Close();
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAddLang_Click(object sender, EventArgs e)
        {
            rmels = new ResourceManagerEditorLanguageSelect(languageselected);
            rmels.ShowDialog();
            languageselected = rmels.SetSelectedLang();
        }

        private void btnReadFromDB_Click(object sender, EventArgs e)
        {
            try
            {
                readDB();
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }
        }

        private void btnWriteToDB_Click(object sender, EventArgs e)
        {
            writeDB();
        }

        private SqlDataAdapter sqlda;
        private OdbcDataAdapter odbcda;
        private OleDbDataAdapter oleda;
        private OracleDataAdapter oracleda;
        private DataSet ds;
        private void readDB()
        {
            string strSelCol = "id, IDENTIFICATION, KEYS";
            if (languageselected[0])
                strSelCol += ", EN";
            if (languageselected[1])
                strSelCol += ", CHT";
            if (languageselected[2])
                strSelCol += ", CHS";
            if (languageselected[3])
                strSelCol += ", HK";
            if (languageselected[4])
                strSelCol += ", JA";
            if (languageselected[5])
                strSelCol += ", KO";
            if (languageselected[6])
                strSelCol += ", LAN1";
            if (languageselected[7])
                strSelCol += ", LAN2";

            if ((ClientType)DBInfo[0] == ClientType.ctMsSql)
            {
                string strCn = DBInfo[1].ToString();
                SqlConnection cn = new SqlConnection(strCn);
                string strSql = "select " + strSelCol + " from SYS_LANGUAGE where IDENTIFICATION = '" + identification.ToString() + "'";
                sqlda = new SqlDataAdapter(strSql, cn);
                ds = new DataSet();
                sqlda.Fill(ds, "languageInfo");
            }
            if ((ClientType)DBInfo[0] == ClientType.ctODBC)
            {
                string strCn = DBInfo[1].ToString();
                OdbcConnection cn = new OdbcConnection(strCn);
                string strSql = "select " + strSelCol + " from SYS_LANGUAGE where IDENTIFICATION = '" + identification.ToString() + "'";
                odbcda = new OdbcDataAdapter(strSql, cn);
                ds = new DataSet();
                odbcda.Fill(ds, "languageInfo");
            }
            if ((ClientType)DBInfo[0] == ClientType.ctOleDB)
            {
                string strCn = DBInfo[1].ToString();
                OleDbConnection cn = new OleDbConnection(strCn);
                string strSql = "select " + strSelCol + " from SYS_LANGUAGE where IDENTIFICATION = '" + identification.ToString() + "'";
                oleda = new OleDbDataAdapter(strSql, cn);
                ds = new DataSet();
                oleda.Fill(ds, "languageInfo");
            }
            if ((ClientType)DBInfo[0] == ClientType.ctOracle)
            {
                string strCn = DBInfo[1].ToString();
                OracleConnection cn = new OracleConnection(strCn);
                string strSql = "select " + strSelCol + " from SYS_LANGUAGE where IDENTIFICATION = '" + identification.ToString() + "'";
                oracleda = new OracleDataAdapter(strSql, cn);
                ds = new DataSet();
                oracleda.Fill(ds, "languageInfo");
            }
            GetCompProp();
            GetViewHeadText();
            GetCollectionValue();
            this.dgViewLanguage.DataSource = ds;
            this.dgViewLanguage.DataMember = "languageInfo";
            this.dgViewLanguage.Columns["id"].Visible = false;
            this.dgViewLanguage.Columns["IDENTIFICATION"].Visible = false;

        }


        private void writeDB()
        {
            if ((ClientType)DBInfo[0] == ClientType.ctMsSql)
            {
                SqlCommandBuilder cmdBulider = new SqlCommandBuilder(sqlda);
                if (ds.HasChanges())
                {
                    sqlda.Update(ds, "languageInfo");
                }
            }
            if ((ClientType)DBInfo[0] == ClientType.ctODBC)
            {
                OdbcCommandBuilder cmdBulider = new OdbcCommandBuilder(odbcda);
                if (ds.HasChanges())
                {
                    odbcda.Update(ds, "languageInfo");
                }
            }
            if ((ClientType)DBInfo[0] == ClientType.ctOleDB)
            {
                OleDbCommandBuilder cmdBulider = new OleDbCommandBuilder(oleda);
                if (ds.HasChanges())
                {
                    oleda.Update(ds, "languageInfo");
                }
            }
            if ((ClientType)DBInfo[0] == ClientType.ctOracle)
            {
                OracleCommandBuilder cmdBulider = new OracleCommandBuilder(oracleda);
                if (ds.HasChanges())
                {
                    oracleda.Update(ds, "languageInfo");
                }
            }
            readDB();


        }

        private void GetCollectionValue()
        {
            System.Web.UI.ControlCollection CompCol = this.MultiLanguage.Page.Controls;
            int comcount = CompCol.Count;
            for (int j = 0; j < comcount; j++)
            {
                #region WebValidate
                if (CompCol[j] is WebValidate)
                {
                    int count = (CompCol[j] as WebValidate).Fields.Count;
                    for (int i = 0; i < count; i++)
                    {
                        int k = this.ds.Tables["languageInfo"].Rows.Count;
                        bool bHasDefined = false;
                        for (int l = 0; l < k; l++)
                        {
                            if (this.ds.Tables["languageInfo"].Rows[l]["keys"].ToString() == CompCol[j].Site.Name + ".Warningmsg" + i.ToString())
                            {
                                bHasDefined = true;
                                break;
                            }
                        }
                        if (!bHasDefined)
                        {
                            this.ds.Tables["languageInfo"].Rows.Add();
                            this.ds.Tables["languageInfo"].Rows[k]["keys"] = CompCol[j].Site.Name + ".Warningmsg" + i.ToString();
                            this.ds.Tables["languageInfo"].Rows[k]["IDENTIFICATION"] = identification.ToString();
                            string defValue = ((CompCol[j] as WebValidate).Fields[i] as ValidateFieldItem).WarningMsg;
                            string strSql = "select * from COLDEF where CAPTION = '" + defValue + "'";
                            if ((ClientType)DBInfo[0] == ClientType.ctMsSql)
                            {
                                string strCn = DBInfo[1].ToString();
                                SqlConnection cn = new SqlConnection(strCn);
                                cn.Open();
                                SqlCommand cmd = new SqlCommand(strSql, cn);
                                SqlDataReader sqldr = cmd.ExecuteReader();
                                SetDDValue(k, sqldr, defValue);
                                cn.Close();
                            }
                            if ((ClientType)DBInfo[0] == ClientType.ctODBC)
                            {
                                string strCn = DBInfo[1].ToString();
                                OdbcConnection cn = new OdbcConnection(strCn);
                                cn.Open();
                                OdbcCommand cmd = new OdbcCommand(strSql, cn);
                                OdbcDataReader odbcdr = cmd.ExecuteReader();
                                SetDDValue(k, odbcdr, defValue);
                                cn.Close();
                            }
                            if ((ClientType)DBInfo[0] == ClientType.ctOleDB)
                            {
                                string strCn = DBInfo[1].ToString();
                                OleDbConnection cn = new OleDbConnection(strCn);
                                cn.Open();
                                OleDbCommand cmd = new OleDbCommand(strSql, cn);
                                OleDbDataReader oledr = cmd.ExecuteReader();
                                SetDDValue(k, oledr, defValue);
                                cn.Close();
                            }
                            if ((ClientType)DBInfo[0] == ClientType.ctOracle)
                            {
                                string strCn = DBInfo[1].ToString();
                                OracleConnection cn = new OracleConnection(strCn);
                                cn.Open();
                                OracleCommand cmd = new OracleCommand(strSql, cn);
                                OracleDataReader oracledr = cmd.ExecuteReader();
                                SetDDValue(k, oracledr, defValue);
                                cn.Close();
                            }
                        }
                    }
                } 
                #endregion

                #region WebClientQuery
                if (CompCol[j] is WebClientQuery)
                {
                    int count = (CompCol[j] as WebClientQuery).Columns.Count;
                    for (int i = 0; i < count; i++)
                    {
                        int k = this.ds.Tables["languageInfo"].Rows.Count;
                        bool bHasDefined = false;
                        for (int l = 0; l < k; l++)
                        {
                            if (this.ds.Tables["languageInfo"].Rows[l]["keys"].ToString() == CompCol[j].Site.Name + ".Caption" + i.ToString())
                            {
                                bHasDefined = true;
                                break;
                            }
                        }
                        if (!bHasDefined)
                        {
                            this.ds.Tables["languageInfo"].Rows.Add();
                            this.ds.Tables["languageInfo"].Rows[k]["keys"] = CompCol[j].Site.Name + ".Caption" + i.ToString();
                            this.ds.Tables["languageInfo"].Rows[k]["IDENTIFICATION"] = identification.ToString();
                            string defValue = ((CompCol[j] as WebClientQuery).Columns[i] as WebQueryColumns).Caption;
                            string strSql = "select * from COLDEF where CAPTION = '" + defValue + "'";
                            if ((ClientType)DBInfo[0] == ClientType.ctMsSql)
                            {
                                string strCn = DBInfo[1].ToString();
                                SqlConnection cn = new SqlConnection(strCn);
                                cn.Open();
                                SqlCommand cmd = new SqlCommand(strSql, cn);
                                SqlDataReader sqldr = cmd.ExecuteReader();
                                SetDDValue(k, sqldr, defValue);
                                cn.Close();
                            }
                            if ((ClientType)DBInfo[0] == ClientType.ctODBC)
                            {
                                string strCn = DBInfo[1].ToString();
                                OdbcConnection cn = new OdbcConnection(strCn);
                                cn.Open();
                                OdbcCommand cmd = new OdbcCommand(strSql, cn);
                                OdbcDataReader odbcdr = cmd.ExecuteReader();
                                SetDDValue(k, odbcdr, defValue);
                                cn.Close();
                            }
                            if ((ClientType)DBInfo[0] == ClientType.ctOleDB)
                            {
                                string strCn = DBInfo[1].ToString();
                                OleDbConnection cn = new OleDbConnection(strCn);
                                cn.Open();
                                OleDbCommand cmd = new OleDbCommand(strSql, cn);
                                OleDbDataReader oledr = cmd.ExecuteReader();
                                SetDDValue(k, oledr, defValue);
                                cn.Close();
                            }
                            if ((ClientType)DBInfo[0] == ClientType.ctOracle)
                            {
                                string strCn = DBInfo[1].ToString();
                                OracleConnection cn = new OracleConnection(strCn);
                                cn.Open();
                                OracleCommand cmd = new OracleCommand(strSql, cn);
                                OracleDataReader oracledr = cmd.ExecuteReader();
                                SetDDValue(k, oracledr, defValue);
                                cn.Close();
                            }
                        }
                    }
                }
                #endregion
            }

        }

        private void GetCompProp()
        {
            System.Web.UI.ControlCollection CompCol = this.MultiLanguage.Page.Controls;
            int i = CompCol.Count;

            for (int j = 0; j < i; j++)
            {
                Type myType = CompCol[j].GetType();
                PropertyInfo[] pi = myType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                int m = pi.Length;
                for (int n = 0; n < m; n++)
                {
                    if (pi[n].PropertyType == typeof(string) && pi[n].GetValue(CompCol[j], null) != null)
                    {

                        if (pi[n].Name.ToLower() == "text" || pi[n].Name.ToLower() == "tooltip")
                        {
                            int k = this.ds.Tables["languageInfo"].Rows.Count;
                            bool bHasDefined = false;
                            for (int l = 0; l < k; l++)
                            {
                                if (this.ds.Tables["languageInfo"].Rows[l]["keys"].ToString() == CompCol[j].Site.Name + "." + pi[n].Name)
                                {
                                    bHasDefined = true;
                                    break;
                                }
                            }
                            if (!bHasDefined)
                            {
                                this.ds.Tables["languageInfo"].Rows.Add();
                                this.ds.Tables["languageInfo"].Rows[k]["keys"] = CompCol[j].Site.Name + "." + pi[n].Name;
                                this.ds.Tables["languageInfo"].Rows[k]["IDENTIFICATION"] = identification.ToString();
                                string defValue = pi[n].GetValue(CompCol[j], null).ToString();

                                string strSql = "select * from COLDEF where CAPTION = '" + defValue + "'";
                                if ((ClientType)DBInfo[0] == ClientType.ctMsSql)
                                {
                                    string strCn = DBInfo[1].ToString();
                                    SqlConnection cn = new SqlConnection(strCn);
                                    cn.Open();
                                    SqlCommand cmd = new SqlCommand(strSql, cn);
                                    SqlDataReader sqldr = cmd.ExecuteReader();
                                    SetDDValue(k, sqldr, defValue);
                                    cn.Close();
                                }
                                if ((ClientType)DBInfo[0] == ClientType.ctODBC)
                                {
                                    string strCn = DBInfo[1].ToString();
                                    OdbcConnection cn = new OdbcConnection(strCn);
                                    cn.Open();
                                    OdbcCommand cmd = new OdbcCommand(strSql, cn);
                                    OdbcDataReader odbcdr = cmd.ExecuteReader();
                                    SetDDValue(k, odbcdr, defValue);
                                    cn.Close();
                                }
                                if ((ClientType)DBInfo[0] == ClientType.ctOleDB)
                                {
                                    string strCn = DBInfo[1].ToString();
                                    OleDbConnection cn = new OleDbConnection(strCn);
                                    cn.Open();
                                    OleDbCommand cmd = new OleDbCommand(strSql, cn);
                                    OleDbDataReader oledr = cmd.ExecuteReader();
                                    SetDDValue(k, oledr, defValue);
                                    cn.Close();
                                }
                                if ((ClientType)DBInfo[0] == ClientType.ctOracle)
                                {
                                    string strCn = DBInfo[1].ToString();
                                    OracleConnection cn = new OracleConnection(strCn);
                                    cn.Open();
                                    OracleCommand cmd = new OracleCommand(strSql, cn);
                                    OracleDataReader oracledr = cmd.ExecuteReader();
                                    SetDDValue(k, oracledr, defValue);
                                    cn.Close();
                                }
                            }
                        }
                    }
                }
            }
        }

        private void GetViewHeadText()
        {
            System.Web.UI.ControlCollection CompCol = this.MultiLanguage.Page.Controls;
            foreach (System.Web.UI.Control ctrl in CompCol)
            {
                if (ctrl is WebGridView)
                {
                    WebGridView wgv = (WebGridView)ctrl;

                    foreach (DataControlField dc in wgv.Columns)
                    {
                        string field = "";
                        if (dc is BoundField)
                            field = ((BoundField)dc).DataField;
                        else if (dc.SortExpression != null && dc.SortExpression != "")
                            field = dc.SortExpression;
                        else
                            continue;

                        int k = this.ds.Tables["languageInfo"].Rows.Count;
                        bool bHasDefined = false;
                        for (int l = 0; l < k; l++)
                        {
                            if (this.ds.Tables["languageInfo"].Rows[l]["keys"].ToString() == wgv.ID + "." + field + ".HeadText")
                            {
                                bHasDefined = true;
                                break;
                            }
                        }
                        if (!bHasDefined)
                        {
                            this.ds.Tables["languageInfo"].Rows.Add();
                            this.ds.Tables["languageInfo"].Rows[k]["keys"] = wgv.ID + "." + field + ".HeadText";
                            this.ds.Tables["languageInfo"].Rows[k]["IDENTIFICATION"] = this.identification.ToString();
                            string defValue = dc.HeaderText;

                            string strSql = "select * from COLDEF where CAPTION = '" + defValue + "'";
                            if ((ClientType)DBInfo[0] == ClientType.ctMsSql)
                            {
                                string strCn = DBInfo[1].ToString();
                                SqlConnection cn = new SqlConnection(strCn);
                                cn.Open();
                                SqlCommand cmd = new SqlCommand(strSql, cn);
                                SqlDataReader sqldr = cmd.ExecuteReader();
                                SetDDValue(k, sqldr, defValue);
                                cn.Close();
                            }
                            if ((ClientType)DBInfo[0] == ClientType.ctODBC)
                            {
                                string strCn = DBInfo[1].ToString();
                                OdbcConnection cn = new OdbcConnection(strCn);
                                cn.Open();
                                OdbcCommand cmd = new OdbcCommand(strSql, cn);
                                OdbcDataReader odbcdr = cmd.ExecuteReader();
                                SetDDValue(k, odbcdr, defValue);
                                cn.Close();
                            }
                            if ((ClientType)DBInfo[0] == ClientType.ctOleDB)
                            {
                                string strCn = DBInfo[1].ToString();
                                OleDbConnection cn = new OleDbConnection(strCn);
                                cn.Open();
                                OleDbCommand cmd = new OleDbCommand(strSql, cn);
                                OleDbDataReader oledr = cmd.ExecuteReader();
                                SetDDValue(k, oledr, defValue);
                                cn.Close();
                            }
                            if ((ClientType)DBInfo[0] == ClientType.ctOracle)
                            {
                                string strCn = DBInfo[1].ToString();
                                OracleConnection cn = new OracleConnection(strCn);
                                cn.Open();
                                OracleCommand cmd = new OracleCommand(strSql, cn);
                                OracleDataReader oracledr = cmd.ExecuteReader();
                                SetDDValue(k, oracledr, defValue);
                                cn.Close();
                            }
                        }
                    }
                }
                else if (ctrl is WebDetailsView)
                {
                    WebDetailsView wdv = (WebDetailsView)ctrl;
                    foreach (System.Web.UI.WebControls.DataControlField dc in wdv.Fields)
                    {
                        string field = "";
                        if (dc is BoundField)
                            field = ((BoundField)dc).DataField;
                        else if (dc.SortExpression != null && dc.SortExpression != "")
                            field = dc.SortExpression;
                        else
                            continue; 
                        
                        int k = this.ds.Tables["languageInfo"].Rows.Count;
                        bool bHasDefined = false;
                        for (int l = 0; l < k; l++)
                        {
                            if (this.ds.Tables["languageInfo"].Rows[l]["keys"].ToString() == wdv.ID + "." + field + ".HeadText")
                            {
                                bHasDefined = true;
                                break;
                            }
                        }
                        if (!bHasDefined)
                        {
                            this.ds.Tables["languageInfo"].Rows.Add();
                            this.ds.Tables["languageInfo"].Rows[k]["keys"] = wdv.ID + "." + field + ".HeadText";
                            this.ds.Tables["languageInfo"].Rows[k]["IDENTIFICATION"] = identification.ToString();
                            string defValue = dc.HeaderText;

                            string strSql = "select * from COLDEF where CAPTION = '" + defValue + "'";
                            if ((ClientType)DBInfo[0] == ClientType.ctMsSql)
                            {
                                string strCn = DBInfo[1].ToString();
                                SqlConnection cn = new SqlConnection(strCn);
                                cn.Open();
                                SqlCommand cmd = new SqlCommand(strSql, cn);
                                SqlDataReader sqldr = cmd.ExecuteReader();
                                SetDDValue(k, sqldr, defValue);
                                cn.Close();
                            }
                            if ((ClientType)DBInfo[0] == ClientType.ctODBC)
                            {
                                string strCn = DBInfo[1].ToString();
                                OdbcConnection cn = new OdbcConnection(strCn);
                                cn.Open();
                                OdbcCommand cmd = new OdbcCommand(strSql, cn);
                                OdbcDataReader odbcdr = cmd.ExecuteReader();
                                SetDDValue(k, odbcdr, defValue);
                                cn.Close();
                            }
                            if ((ClientType)DBInfo[0] == ClientType.ctOleDB)
                            {
                                string strCn = DBInfo[1].ToString();
                                OleDbConnection cn = new OleDbConnection(strCn);
                                cn.Open();
                                OleDbCommand cmd = new OleDbCommand(strSql, cn);
                                OleDbDataReader oledr = cmd.ExecuteReader();
                                SetDDValue(k, oledr, defValue);
                                cn.Close();
                            }
                            if ((ClientType)DBInfo[0] == ClientType.ctOracle)
                            {
                                string strCn = DBInfo[1].ToString();
                                OracleConnection cn = new OracleConnection(strCn);
                                cn.Open();
                                OracleCommand cmd = new OracleCommand(strSql, cn);
                                OracleDataReader oracledr = cmd.ExecuteReader();
                                SetDDValue(k, oracledr, defValue);
                                cn.Close();
                            }
                        }
                    }
                }
                else if (ctrl is WebFormView)
                {
                    WebFormView wfv = (WebFormView)ctrl;
                    string strtemp = EditionDifference.ActiveDocumentFullName() + ".tmp";
                    if(File.Exists(strtemp))
                    {
                        Hashtable ht = new Hashtable();
                        FileStream fs = new FileStream(strtemp, FileMode.Open);
                        StreamReader sr = new StreamReader(fs);
                        while (sr.Peek() != -1)
                        {
                            string line = sr.ReadLine();
                            string name = line.Substring(0, line.IndexOf('='));
                            string value = line.Substring(line.IndexOf('=') + 1);
                            ht.Add(name, value);
                        }
                        sr.Close();
                        fs.Close();

                        foreach( string str in ht.Keys )
                        { 
                            int k = this.ds.Tables["languageInfo"].Rows.Count;
                            bool bHasDefined = false;
                            for (int l = 0; l < k; l++)
                            { 
                                if (this.ds.Tables["languageInfo"].Rows[l]["keys"].ToString() == str)
                                {
                                    bHasDefined = true;
                                    break;
                                }
                            }
                            if (!bHasDefined)
                            {
                                this.ds.Tables["languageInfo"].Rows.Add();
                                this.ds.Tables["languageInfo"].Rows[k]["keys"] = str;
                                this.ds.Tables["languageInfo"].Rows[k]["IDENTIFICATION"] = identification.ToString();
                                string defValue = ht[str].ToString();

                                string strSql = "select * from COLDEF where CAPTION = '" + defValue + "'";
                                if ((ClientType)DBInfo[0] == ClientType.ctMsSql)
                                {
                                    string strCn = DBInfo[1].ToString();
                                    SqlConnection cn = new SqlConnection(strCn);
                                    cn.Open();
                                    SqlCommand cmd = new SqlCommand(strSql, cn);
                                    SqlDataReader sqldr = cmd.ExecuteReader();
                                    SetDDValue(k, sqldr, defValue);
                                    cn.Close();
                                }
                                if ((ClientType)DBInfo[0] == ClientType.ctODBC)
                                {
                                    string strCn = DBInfo[1].ToString();
                                    OdbcConnection cn = new OdbcConnection(strCn);
                                    cn.Open();
                                    OdbcCommand cmd = new OdbcCommand(strSql, cn);
                                    OdbcDataReader odbcdr = cmd.ExecuteReader();
                                    SetDDValue(k, odbcdr, defValue);
                                    cn.Close();
                                }
                                if ((ClientType)DBInfo[0] == ClientType.ctOleDB)
                                {
                                    string strCn = DBInfo[1].ToString();
                                    OleDbConnection cn = new OleDbConnection(strCn);
                                    cn.Open();
                                    OleDbCommand cmd = new OleDbCommand(strSql, cn);
                                    OleDbDataReader oledr = cmd.ExecuteReader();
                                    SetDDValue(k, oledr, defValue);
                                    cn.Close();
                                }
                                if ((ClientType)DBInfo[0] == ClientType.ctOracle)
                                {
                                    string strCn = DBInfo[1].ToString();
                                    OracleConnection cn = new OracleConnection(strCn);
                                    cn.Open();
                                    OracleCommand cmd = new OracleCommand(strSql, cn);
                                    OracleDataReader oracledr = cmd.ExecuteReader();
                                    SetDDValue(k, oracledr, defValue);
                                    cn.Close();
                                }
                            }
                        }
                        File.Delete(strtemp);
                    } 
                }
            }
        }

        private object[] ConfirmDataBaseStyle(string sDB)
        {
            object[] objParm = new object[2];
            objParm[0] = null; // ClientType
            objParm[1] = null; // DBstring
            RegistryKey rk = Registry.LocalMachine.OpenSubKey("SOFTWARE\\infolight\\eep.net");
            String s = (String)rk.GetValue("Server Path");
            if (s[s.Length - 1] != '\\')
                s = s + "\\DB.xml";
            else
                s = s + "DB.xml";
            rk.Close();

            XmlDocument DBXML = new XmlDocument();
            if (File.Exists(s))
            {
                DBXML.Load(s);
                if (DBXML != null)
                {
                    XmlNode root = DBXML.FirstChild;
                    if (root != null)
                    {
                        XmlNode aDataBase = root.SelectSingleNode("DataBase");
                        if (aDataBase != null)
                        {
                            XmlNode aDBConn = aDataBase.SelectSingleNode(sDB);
                            if (aDBConn != null)
                            {
                                objParm[0] = (ClientType)(Int32.Parse(aDBConn.Attributes["Type"].InnerText));
                                objParm[1] = aDBConn.Attributes["String"].InnerText + ";password="
                                    + GetPwdString(aDBConn.Attributes["Password"].InnerText);
                            }
                        }
                    }
                }
            }
            return objParm;
        }

        private void SetDDValue(int rowIndex, IDataReader reader, string defValue)
        {
            if (reader.Read())
            {
                if (languageselected[0])
                {
                    if (reader["CAPTION1"] != null && reader["CAPTION1"].ToString() != "")
                        this.ds.Tables["languageInfo"].Rows[rowIndex]["en"] = reader["CAPTION1"];
                    else
                        this.ds.Tables["languageInfo"].Rows[rowIndex]["en"] = defValue;
                }
                if (languageselected[1])
                {
                    if (reader["CAPTION2"] != null && reader["CAPTION2"].ToString() != "")
                        this.ds.Tables["languageInfo"].Rows[rowIndex]["cht"] = reader["CAPTION2"];
                    else
                        this.ds.Tables["languageInfo"].Rows[rowIndex]["cht"] = defValue;
                }
                if (languageselected[2])
                {
                    if (reader["CAPTION3"] != null && reader["CAPTION3"].ToString() != "")
                        this.ds.Tables["languageInfo"].Rows[rowIndex]["chs"] = reader["CAPTION3"];
                    else
                        this.ds.Tables["languageInfo"].Rows[rowIndex]["chs"] = defValue;
                }
                if (languageselected[3])
                {
                    if (reader["CAPTION4"] != null && reader["CAPTION4"].ToString() != "")
                        this.ds.Tables["languageInfo"].Rows[rowIndex]["hk"] = reader["CAPTION4"];
                    else
                        this.ds.Tables["languageInfo"].Rows[rowIndex]["hk"] = defValue;
                }
                if (languageselected[4])
                {
                    if (reader["CAPTION5"] != null && reader["CAPTION5"].ToString() != "")
                        this.ds.Tables["languageInfo"].Rows[rowIndex]["ja"] = reader["CAPTION5"];
                    else
                        this.ds.Tables["languageInfo"].Rows[rowIndex]["ja"] = defValue;
                }
                if (languageselected[5])
                {
                    if (reader["CAPTION6"] != null && reader["CAPTION6"].ToString() != "")
                        this.ds.Tables["languageInfo"].Rows[rowIndex]["ko"] = reader["CAPTION6"];
                    else
                        this.ds.Tables["languageInfo"].Rows[rowIndex]["ko"] = defValue;
                }
                if (languageselected[6])
                {
                    if (reader["CAPTION7"] != null && reader["CAPTION7"].ToString() != "")
                        this.ds.Tables["languageInfo"].Rows[rowIndex]["lan1"] = reader["CAPTION7"];
                    else
                        this.ds.Tables["languageInfo"].Rows[rowIndex]["lan1"] = defValue;
                }
                if (languageselected[7])
                {
                    if (reader["CAPTION8"] != null && reader["CAPTION8"].ToString() != "")
                        this.ds.Tables["languageInfo"].Rows[rowIndex]["lan2"] = reader["CAPTION8"];
                    else
                        this.ds.Tables["languageInfo"].Rows[rowIndex]["lan2"] = defValue;
                }
            }
            else
            {
                if (languageselected[0])
                    this.ds.Tables["languageInfo"].Rows[rowIndex]["en"] = defValue;
                if (languageselected[1])
                    this.ds.Tables["languageInfo"].Rows[rowIndex]["cht"] = defValue;
                if (languageselected[2])
                    this.ds.Tables["languageInfo"].Rows[rowIndex]["chs"] = defValue;
                if (languageselected[3])
                    this.ds.Tables["languageInfo"].Rows[rowIndex]["hk"] = defValue;
                if (languageselected[4])
                    this.ds.Tables["languageInfo"].Rows[rowIndex]["ja"] = defValue;
                if (languageselected[5])
                    this.ds.Tables["languageInfo"].Rows[rowIndex]["ko"] = defValue;
                if (languageselected[6])
                    this.ds.Tables["languageInfo"].Rows[rowIndex]["lan1"] = defValue;
                if (languageselected[7])
                    this.ds.Tables["languageInfo"].Rows[rowIndex]["lan2"] = defValue;
            }
        }

        private static string GetPwdString(string s)
        {
            string sRet = "";
            for (int i = 0; i < s.Length; i++)
            {
                sRet = sRet + (char)(((int)(s[s.Length - 1 - i])) ^ s.Length);
            }
            return sRet;
        }

        private void dgViewLanguage_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            this.dgViewLanguage["identification", e.Row.Index - 1].Value = this.identification.ToString();
        }
    }
}